<?php
/**
 * 下载内容
 * @author Mortimer He
 */
namespace Manage\Controller;
use Common\Library\Page;
use Common\Library\Upload;
class DownloadController extends CommonController{
	private $msg=array();
	private $Model;
	function _initialize(){
		parent::_initialize();
		$this->Model=D('Download');
	}
	public function index(){	
		if(!$this->checkPermission('Download')) $this->jump();	
		$filter_data=array();
		if(I('get.name','','trim')){
			$filter_data['name'] = I('get.name','','trim');
			$url['name']=I('get.name','','trim');
		}
		$url['p']=I('get.p','','trim');
		$count=$this->Model->getDownloadTotal($filter_data);
		$Page = new Page($count,C('CONFIG_LIMIT_MANAGE'));
		$Page->setConfig('header',L('page_header'));
		$Page->setConfig('theme',L('page_theme'));
		$filter_data['start']=$Page->firstRow;
		$filter_data['limit']=$Page->listRows;
		$list=$this->Model->getDownloads($filter_data);
		$show=$Page->show();
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->assign('url',$url);
		$this->display();
	}

	public function oper($download_id=0){
		if(!$download_id && !$this->checkPermission('DownloadAdd')) $this->jump();
		$download=array();
		if($download_id){
			$download=$this->Model->getDownload($download_id);
			if(empty($download)) $this->jump(L('error_404'));
		}
		if(IS_POST){
			$download=I('post.');
			if($this->validateForm()){
				$this->Model->operDownload($download,$download_id);
				session('success',$download_id?L('success_edit'):L('success_add'));
				$url=array(
					'name'	=>	I('get.name','','trim'),
					'p'		=>	I('get.p','')
					);
				$this->redirect('index',$url);
			}else{
				if(!isset($this->msg['warning'])) $this->msg['warning']=L('error_warning');
			}
		}
		$languages=get_langs();
		$this->assign('download',$download);
		$this->assign('msg',$this->msg);
		$this->assign('languages',$languages);
		$this->display();
	}

	public function upload(){
		$Upload = new Upload();
		$Upload->maxSize   =     102400 ;
		$Upload->saveName  =	array('set_filename','__FILE__');
		// 设置附件上传类型
    	$Upload->exts      =     array('zip', 'pdf', 'xls', 'doc','jpg','png');
    	// 设置附件上传根目录
    	$Upload->rootPath  =     C('DOWNLOAD_DIR'); 
    	$Upload->subName   =	'trim';
    	$Upload->savePath  = 	'';
    	// 上传文件 
	    $info   =   $Upload->uploadOne($_FILES['file']);
	    if(!$info) {
	    	// 上传错误提示错误信息
	        $json['error']=$Upload->getError();
	    }else{
	    	// 上传成功
	        $json=array(
	        	'success'	=>	L('success_upload'),
	        	'filename'	=>	$info['savename'],
	        	'mask'		=>	$info['name']
	        	);
	    }
	    $this->ajaxReturn($json,'JSON');
	}
	public function del(){
		if(!$this->checkPermission('DownloadDelete')){
			session('danger',L('error_not_permission'));
		}elseif($this->validateDelete()){
			$ids=I('post.selected');
			$count=0;
			foreach ($ids as $download_id) {
				$count += $this->Model->del($download_id);
			}
			session('success',L('success_delete',array('count'=>$count)));
		}else{
			
		}
		$url['p']=I('get.p','');
		$this->redirect('index',$url);
	}
	private function validateDelete(){
		return true;
	}
	private function validateForm(){
		$download_id=I('get.download_id',0,'trim');
		if($download_id && !$this->checkPermission('DownloadEdit')){
			$this->msg['warning']	=	L('error_not_permission');
		}else{
			$data=I('post.');
			foreach ($data['download_description'] as $language_id => $v){
				if(mb_strlen($v['name'])<2 || mb_strlen($v['name'])>100) $this->msg['name'][$language_id]=L('error_name');
			}
			if(mb_strlen($data['mask'])>128 || mb_strlen($data['mask'])<3) $this->msg['mask']=L('error_mask');
		}
		return !$this->msg;
	}

	public function autocomplete($filter_name=''){
		$data=array(
			'start'	=>	0,
			'limit'	=>	8,
			'name'	=>	$filter_name
			);
		$json=$this->Model->getDownloads($data);
		$this->ajaxReturn($json,'JSON');
	}
}
